<template>
  <div class="article-list">
    <div
      class="article-item"
      v-for="item in articles"
      :key="item.articleId"
      @click="goToDetail(item.articleId)"
    >
      <div class="text">
        <h3 class="title">{{ item.title }}</h3>
        <div class="meta">
          <span class="author">{{ item.authorName }}</span>
          <span class="comments">🗨️ {{ item.commentCount }}</span>
        </div>
      </div>
      <div class="cover">
        <img :src="item.cover" alt="封面图" />
      </div>
    </div>
  </div>
  <!-- 统一加载更多样式 -->
  <div class="load-more-wrapper">
    <div v-if="hasMoreArticles" class="load-more" @click="loadMoreArticles">
      <span class="load-text" :class="{ loading }">
        {{ loading ? '加载中...' : '点击加载更多' }}
      </span>
    </div>
    <div v-else class="no-more">
      <span>没有更多内容了</span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getAllArticle } from '@/apis/article'

// 声明响应式文章列表
const articles = ref([])

// 路由实例
const router = useRouter()

// 获取文章列表
// const fetchArticles = async () => {
//   try {
//     const res = await getAllArticle()
//     articles.value = res.data || []
//   } catch (err) {
//     console.error('获取文章失败:', err)
//   }
// }

// 跳转文章详情
const goToDetail = (articleId) => {
  router.push(`/home/article/${articleId}`)
}

// 生命周期钩子
onMounted(() => {
  loadArticles()
})

// 文章分页逻辑
const articlePage = ref(1)
const hasMoreArticles = ref(true)
const size = ref(3)
const loading = ref(false)
const loadArticles = async () => {
  const res = await getAllArticle({
    pageNum: articlePage.value,
    pageSize: size.value,
  })

  if (res.data?.length > 0) {
    articles.value = [...articles.value, ...res.data]
    hasMoreArticles.value = res.data.length === size.value
  } else {
    hasMoreArticles.value = false
  }
}

const loadMoreArticles = async () => {
  if (!hasMoreArticles.value || loading.value) return
  try {
    loading.value = true
    articlePage.value++
    await loadArticles()
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.article-list {
  max-height: 100vh; /* 视口高度的70% */
  overflow-y: auto; /* 垂直滚动 */
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding-right: 8px; /* 防止滚动条覆盖内容 */

  /* 可选：自定义滚动条样式 */
  scrollbar-width: thin;
  scrollbar-color: #c1c1c1 #f5f5f5;
}

.article-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 16px;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
  cursor: pointer;
  transition: background-color 0.2s, box-shadow 0.2s;
}

.article-item:hover {
  background-color: #f0f9ff;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.text {
  flex: 1;
  margin-right: 12px;
}

.title {
  font-size: 16px;
  font-weight: bold;
  margin: 0;
}

.meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #666;
  margin-top: 4px;
}

.author {
  flex-shrink: 0;
}

.comments {
  flex-shrink: 0;
}

.cover img {
  width: 100px;
  height: 60px;
  object-fit: cover;
  border-radius: 6px;
}
.load-more-wrapper {
  margin: 20px 0;
  text-align: center;
}

/* 统一加载/提示区域样式 */
.load-more,
.no-more {
  display: inline-block;
  padding: 12px 24px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

/* 加载更多具体样式 */
.load-more {
  background-color: #f5f5f5;
  color: #666;
}

/* 没有更多提示样式 */
.no-more {
  background-color: transparent;
  color: #999;
  cursor: default;
  padding: 12px 0; /* 减少左右padding */
}
</style>
